
<%
//note: DB.asp not included here because it is included in the page that also includes this model

var AdminScriptEditorModel = function(){	
	
	this.ScriptID = Request.QueryString("ScriptID") || "new";
	this.Title = "";
	this.Category ="";
	this.InitialScriptBlock = {};
	this.InitialDecisions = [];
	this.Categories = [];
	this.Appliances = [];
	
	/*----------------------------------------------------------------------*/
	this.Init = function(){

		this.LoadScript();
		this.LoadInitialScriptBlock();
		this.LoadInitialDecisions();
		this.LoadCategories();
		this.LoadAppliances();
	}
	
	/*----------------------------------------------------------------------*/
	this.LoadScript = function(){
		if(this.ScriptID != "new" && this.ScriptID != ""){
			var db, rs;
			db = new DB.DataCall();
			db.QueryName =			"qryGetScript";
			db.ConnectionString =	DB.ConnectionStrings.Scripts;
			db.CommandType =		DB.CommandTypes.StoredProc;
			
			db.AddParameter("@ScriptID", DB.DataTypes.String, this.ScriptID);
			
			db.Execute();	
			
			if(db.Success){
				if(db.RecordSet.RecordCount > 0){
					this.ScriptID =	db.RecordSet.fields("ScriptID").value; //doing this just for a sanity check that the db is returning the right value for ScriptID
					this.Title = 	Helpers.DecodeHTML(db.RecordSet.fields("Title").value);
					this.Category = Helpers.DecodeHTML(db.RecordSet.fields("Category").value);
				}
				else{
					this.ScriptID = "new";
					return false;
				}
			}
			else{
				this.ScriptID = "new";
			}
		}
	}
	
	/*----------------------------------------------------------------------*/
	this.LoadInitialScriptBlock = function(){
		if(this.ScriptID != "new" && this.ScriptID != ""){
			var db, rs;
			db = new DB.DataCall();
			db.QueryName =			"qryGetScriptBlockByDecisionID";
			db.ConnectionString =	DB.ConnectionStrings.Scripts;
			db.CommandType = 		DB.CommandTypes.StoredProc;
			
			db.AddParameter("@ScriptID", DB.DataTypes.String, this.ScriptID);
			db.AddParameter("@DecisionID", DB.DataTypes.String, "0"); //hardcoded 0 because a scripts first script block is always assigned to decision 0
			
			db.Execute();	
			
			if(db.Success){
				if(db.RecordSet.RecordCount > 0){
					this.InitialScriptBlock = {
						ScriptBlockID:	db.RecordSet.fields("ScriptBlockID").value,
						DecisionID:		db.RecordSet.fields("DecisionID").value,
						ScriptText:		Helpers.DecodeHTML(db.RecordSet.fields("ScriptText").value)
					}	
				}
				else{
					this.InitialScriptBlock = {
						ScriptBlockID:	"new",
						DecisionID:		"0",
						ScriptText:		"New script."
					}
					return false;
				}
			}
			else{
				this.InitialScriptBlock = {
					ScriptBlockID:	"new",
					DecisionID:		"0",
					ScriptText:		"New script."
				}
			}


		}
		else{
			this.InitialScriptBlock = {
				ScriptBlockID:	"new",
				DecisionID:		"0",
				ScriptText:		"New script."
			}
		}
	}
	
	/*----------------------------------------------------------------------*/
	this.LoadInitialDecisions = function(){
		if(this.InitialScriptBlock.ScriptBlockID && this.InitialScriptBlock.ScriptBlockID != "new" && this.InitialScriptBlock.ScriptBlockID != ""){
			var db, rs;
			db = new DB.DataCall();
			db.QueryName =			"qryGetDecisionsAndChildScriptBlock";
			db.ConnectionString =	DB.ConnectionStrings.Scripts;
			db.CommandType =		DB.CommandTypes.StoredProc;
			
			db.AddParameter("@ScriptBlockID", DB.DataTypes.String, this.InitialScriptBlock.ScriptBlockID);
			
			db.Execute();	

			if(db.Success){
				if(db.RecordSet.RecordCount > 0){
					var rs = db.RecordSet;
					rs.MoveFirst();
					
					while(!rs.EOF){
						var decision = {
							DecisionID:			rs.fields("DecisionID").value,
							ScriptBlockID:		rs.fields("ScriptBlockID").value,
							ChildScriptBlockID:	rs.fields("ChildScriptBlockID").value || "",
							DecisionTitle:		Helpers.DecodeHTML(rs.fields("DecisionTitle").value)
						}

						this.InitialDecisions.push(decision);
						
						rs.MoveNext();
					}
	
				}//recordcount > 0
			}//db.Success
		}//ScriptBlockID != "new"
	}
	
	/*----------------------------------------------------------------------*/
	this.LoadCategories = function(){
		var db, rs;
		db = new DB.DataCall();
		db.QueryName =			"qryGetUniqueCategories";
		db.ConnectionString =	DB.ConnectionStrings.Scripts;
		db.CommandType =		DB.CommandTypes.StoredProc;
		
	    db.Execute();
	    
		if(db.Success){
			if(db.RecordSet.RecordCount > 0){
				var rs = db.RecordSet;
				rs.MoveFirst();
				
				while(!rs.EOF){
					this.Categories.push(Helpers.DecodeHTML(rs.fields("Category").value));
					
					rs.MoveNext();
				}

			}//recordcount > 0
		}//db.Success
    }

    /*----------------------------------------------------------------------*/
	this.LoadAppliances = function(){
		var db, rs;
		db = new DB.DataCall();
		db.QueryName =			"qryGetUniqueAppliances";
		db.ConnectionString =	DB.ConnectionStrings.Scripts;
		db.CommandType =		DB.CommandTypes.StoredProc;
		
	    db.Execute();
	    
		if(db.Success){
			if(db.RecordSet.RecordCount > 0){
				var rs = db.RecordSet;
				rs.MoveFirst();
				
				while(!rs.EOF){

					this.Appliances.push(Helpers.DecodeHTML(rs.fields("Appliance").value));
					
					rs.MoveNext();
				}

			}//recordcount > 0
		}//db.Success
	}
}

%>